package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.support.annotation.IntRange;
import android.text.InputFilter;
import android.text.LoginFilter;
import android.text.TextUtils;
import android.widget.TextView;

import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.util.InputUtil;
import lib.quasar.util.ToastUtil;
import lib.quasar.widget.edit.AlterEditText;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 修改数据, 上下2个, 构成一个区间值
 * created by kalu on 2018/4/16 13:15
 */
public class InputDoubleDialog extends BaseDialog {

    /****************************************/

    private OnDialogChangeListener listener;

    public InputDoubleDialog(Activity activity) {
        super(activity);
    }

    @Override
    public void show() {
        super.show();

        final AlterEditText edit = findViewById(R.id.dialog_input_double_input_top);
        if (null == edit) return;

        edit.requestFocus();
        InputUtil.openKeybord(edit);
    }

    @Override
    public void dismiss() {

        final AlterEditText edit = findViewById(R.id.dialog_input_double_input_top);
        if (null != edit) {
            InputUtil.closeKeybord(edit);
        }
        super.dismiss();
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_input_double;
    }

    @Override
    protected void initData() {

        final NavigationView menu = findViewById(R.id.dialog_input_double_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {

            if (isBack) {
                dismiss();
            } else if (isMenu) {
                if (null != listener) {
                    final AlterEditText edit1 = findViewById(R.id.dialog_input_double_input_top);
                    final AlterEditText edit2 = findViewById(R.id.dialog_input_double_input_bottom);
                    if (null != edit1 || null != edit2) {
                        final String input1 = edit1.getText().toString().trim();
                        final String input2 = edit2.getText().toString().trim();
                        if (TextUtils.isEmpty(input1)) {
                            ToastUtil.showToast("输入数值不能为空");
                            return;
                        }

                        if (TextUtils.isEmpty(input2)) {
                            ToastUtil.showToast("输入数值不能为空");
                            return;
                        }
                        listener.onDialogChange(input1, input2);
                    }
                }

                dismiss();
            }
        });
    }

    public final void setTitle(String name) {
        final NavigationView text1 = findViewById(R.id.dialog_input_double_menu);
        if (null == text1) return;
        text1.setTitle(name);
    }

    public final void setMenu(String name1, String name2) {

        final TextView text1 = findViewById(R.id.dialog_input_double_text_top);
        if (null != text1) {
            text1.setText(name1);
        }

        final TextView text2 = findViewById(R.id.dialog_input_double_text_bottom);
        if (null != text2) {
            text2.setText(name2);
        }
    }

    public final void setMenu(int name1, int name2) {

        final TextView text1 = findViewById(R.id.dialog_input_double_text_top);
        if (null != text1) {
            text1.setText(name1);
        }

        final TextView text2 = findViewById(R.id.dialog_input_double_text_bottom);
        if (null != text2) {
            text2.setText(name2);
        }
    }

    public final void setInput(String input1, String input2) {
        if (!TextUtils.isEmpty(input1)) {
            final AlterEditText text1 = findViewById(R.id.dialog_input_double_input_top);
            if (null != text1) {
                text1.setText(input1);
                text1.setSelection(input1.length());
            }
        }

        if (!TextUtils.isEmpty(input2)) {
            final AlterEditText text2 = findViewById(R.id.dialog_input_double_input_bottom);
            if (null != text2) {
                text2.setText(input2);
                text2.setSelection(input2.length());
            }
        }
    }

    public final void setInputType(int type1, int type2) {

        final AlterEditText edit1 = findViewById(R.id.dialog_input_double_input_top);
        edit1.setInputType(type1);

        final AlterEditText edit2 = findViewById(R.id.dialog_input_double_input_bottom);
        edit2.setInputType(type2);
    }

    public void setNumberFilter(@IntRange(from = 1, to = Integer.MAX_VALUE) int dotLength,
                                @IntRange(from = 1, to = Integer.MAX_VALUE) int inputLength) {

        final AlterEditText edit1 = findViewById(R.id.dialog_input_double_input_top);
        edit1.setNumberFilter(dotLength, inputLength);

        final AlterEditText edit2 = findViewById(R.id.dialog_input_double_input_bottom);
        edit2.setNumberFilter(dotLength, inputLength);
    }

    public final void setOnDialogChangeListener(OnDialogChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDialogChangeListener {

        void onDialogChange(String top, String bottom);
    }
}
